Passed
Push — master ( 0d18ca...6ca9b2 )
by Rafael S.
02:37
created

RIFFFile.getSubChunksIndex_   A

Complexity

Conditions 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
/*
2
 * Copyright (c) 2017-2019 Rafael da Silva Rocha.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/**
26
 * @fileoverview The RIFFFile class.
27
 * @see https://github.com/rochars/wavefile
28
 */
29
30
/** @module wavefile */
31
32
import {unpackString, unpack} from 'byte-data';
33
34
/**
35
 * A class to perform low-level reading of RIFF/RIFX files.
36
 */
37
export default class RIFFFile {
38
39
  constructor() {
40
    /**
41
     * The container identifier.
42
     * 'RIFF', 'RIFX' and 'RF64' are supported.
43
     * @type {string}
44
     */
45
    this.container = '';
46
    /**
47
     * @type {number}
48
     */
49
    this.chunkSize = 0;
50
    /**
51
     * The format.
52
     * @type {string}
53
     */
54
    this.format = '';
55
    /**
56
     * A object defining the start and end of all chunks in a wav buffer.
57
     * @type {!Object}
58
     */
59
    this.signature = {};
60
    /**
61
     * @type {number}
62
     * @protected
63
     */
64
    this.head = 0;
65
    /**
66
     * @type {!Object}
67
     * @protected
68
     */
69
    this.uInt32 = {bits: 32, be: false};
70
    /**
71
     * The list of supported containers.
72
     * Any format different from RIFX will be treated as RIFF.
73
     * @type {!Array<string>}
74
     * @protected
75
     */
76
    this.supported_containers = ['RIFF', 'RIFX'];
77
  }
78
79
  /**
80
   * Read the signature of the chunks in a RIFF/RIFX file.
81
   * @param {!Uint8Array} buffer The file bytes.
82
   * @protected
83
   */
84
  setSignature(buffer) {
85
      this.head = 0;
86
      this.container = this.readString(buffer, 4);
87
      if (this.supported_containers.indexOf(this.container) === -1) {
88
        throw Error('Not a supported format.');
89
      }
90
      this.uInt32.be = this.container === 'RIFX';
91
      this.chunkSize = this.readUInt32(buffer);
92
      this.format = this.readString(buffer, 4);
93
      // The RIFF file signature
94
      this.signature = {
95
          chunkId: this.container,
96
          chunkSize: this.chunkSize,
97
          format: this.format,
98
          subChunks: this.getSubChunksIndex_(buffer)
99
      };
100
  }
101
102
  /**
103
    * Find a chunk by its fourCC_ in a array of RIFF chunks.
104
    * @param {string} chunkId The chunk fourCC_.
105
    * @param {boolean} multiple True if there may be multiple chunks
106
    *    with the same chunkId.
107
    * @return {Object}
108
    * @protected
109
    */
110
  findChunk(chunkId, multiple=false) {
111
    /** @type {!Array<!Object>} */
112
    let chunks = this.signature.subChunks;
113
    /** @type {!Array<!Object>} */
114
    let chunk = [];
115
    for (let i=0; i<chunks.length; i++) {
116
      if (chunks[i].chunkId == chunkId) {
117
        if (multiple) {
118
          chunk.push(chunks[i]);
119
        } else {
120
          return chunks[i];
121
        }
122
      }
123
    }
124
    if (chunkId == 'LIST') {
125
      return chunk.length ? chunk : null;
126
    }
127
    return null;
128
  }
129
130
  /**
131
   * Read bytes as a string from a RIFF chunk.
132
   * @param {!Uint8Array} bytes The bytes.
133
   * @param {number} maxSize the max size of the string.
134
   * @return {string} The string.
135
   * @protected
136
   */
137
  readString(bytes, maxSize) {
138
    /** @type {string} */
139
    let str = '';
140
    str = unpackString(bytes, this.head, this.head + maxSize);
141
    this.head += maxSize;
142
    return str;
143
  }
144
145
  /**
146
   * Read a number from a chunk.
147
   * @param {!Uint8Array} bytes The chunk bytes.
148
   * @return {number} The number.
149
   * @protected
150
   */
151
  readUInt32(bytes) {
152
    /** @type {number} */
153
    let value = unpack(bytes, this.uInt32, this.head);
154
    this.head += 4;
155
    return value;
156
  }
157
158
  /**
159
   * Return the sub chunks of a RIFF file.
160
   * @param {!Uint8Array} buffer the RIFF file bytes.
161
   * @return {!Array<Object>} The subchunks of a RIFF/RIFX or LIST chunk.
162
   * @private
163
   */
164
  getSubChunksIndex_(buffer) {
165
      /** @type {!Array<!Object>} */
166
      let chunks = [];
167
      /** @type {number} */
168
      let i = this.head;
169
      while(i <= buffer.length - 8) {
170
          chunks.push(this.getSubChunkIndex_(buffer, i));
171
          i += 8 + chunks[chunks.length - 1].chunkSize;
172
          i = i % 2 ? i + 1 : i;
173
      }
174
      return chunks;
175
  }
176
177
  /**
178
   * Return a sub chunk from a RIFF file.
179
   * @param {!Uint8Array} buffer the RIFF file bytes.
180
   * @param {number} index The start index of the chunk.
181
   * @return {!Object} A subchunk of a RIFF/RIFX or LIST chunk.
182
   * @private
183
   */
184
  getSubChunkIndex_(buffer, index) {
185
      /** @type {!Object} */
186
      let chunk = {
187
          chunkId: this.getChunkId_(buffer, index),
188
          chunkSize: this.getChunkSize_(buffer, index),
189
      };
190
      if (chunk.chunkId == 'LIST') {
191
          chunk.format = unpackString(buffer, index + 8, index + 12);
192
          this.head += 4;
193
          chunk.subChunks = this.getSubChunksIndex_(buffer);
194
      } else {
195
          /** @type {number} */
196
          let realChunkSize = chunk.chunkSize % 2 ?
197
              chunk.chunkSize + 1 : chunk.chunkSize;
198
          this.head = index + 8 + realChunkSize;
199
          chunk.chunkData = {
200
              start: index + 8,
201
              end: this.head
202
          };
203
      }
204
      return chunk;
205
  }
206
207
  /**
208
   * Return the fourCC_ of a chunk.
209
   * @param {!Uint8Array} buffer the RIFF file bytes.
210
   * @param {number} index The start index of the chunk.
211
   * @return {string} The id of the chunk.
212
   * @private
213
   */
214
  getChunkId_(buffer, index) {
215
      this.head += 4;
216
      return unpackString(buffer, index, index + 4);
217
  }
218
219
  /**
220
   * Return the size of a chunk.
221
   * @param {!Uint8Array} buffer the RIFF file bytes.
222
   * @param {number} index The start index of the chunk.
223
   * @return {number} The size of the chunk without the id and size fields.
224
   * @private
225
   */
226
  getChunkSize_(buffer, index) {
227
      this.head += 4;
228
      return unpack(buffer, this.uInt32, index + 4);
229
  }
230
}
231